package 排序算法;

public class insertSort {
    static int[] arr1 = {3,5,1,-2,9,8};
    static int[] arr2 = {-2,1,3,5,8,9};
    public static void main(String[] args) {
        boolean b = TrueOrFalse();
        if(b){
            Speed();
        }else{
            print(arr1);
        }
    }

    /*
     * 插入排序
     * */
    public static void insertSort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int insert = arr[i];// 待插入的元素
            int insertIndex = i-1;// 待插入元素前一个位置索引
            while(insertIndex >= 0 && insert<arr[insertIndex]){
                arr[insertIndex+1] = arr[insertIndex];
                insertIndex--;
            }
            arr[insertIndex+1] = insert;
        }
    }

    public static boolean TrueOrFalse(){
        insertSort(arr1);
        for (int i = 0; i < arr1.length; i++) {
            if(arr1[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }
    public static void print(int [] arr){
        for (int j : arr) {
            System.out.print(j + " ");
        }
        System.out.println();
    }
    private static void Speed() {
        int[] arr2 = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr2[i] = (int)(Math.random()*80000);
        }
        long begin = System.currentTimeMillis();
        insertSort(arr2);
        long end = System.currentTimeMillis();
        System.out.println((end-begin));
    }
}
